package portal.ejb;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.log4j.Logger;

import portal.converter.convVo;
import portal.entities.Config;
import portal.entities.Producto;
import portal.vo.ConfigVO;

/**
 * Session Bean implementation class AdministradorConfigBean
 */
@Stateless
public class AdministradorConfigsBean implements AdministradorConfigs {
	
	private static Logger logger = Logger.getLogger(AdministradorUsuariosBean.class);

	@PersistenceContext(unitName = "PortalDeComprasDB")
    EntityManager em;
	
    
    /**
     * Default constructor. 
     */
    public AdministradorConfigsBean() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public List<Config> getConfigs(){
    	List<Config> configs = new ArrayList<Config>();
		
		Query query = em.createQuery("from Config");
		configs = query.getResultList();	
		return configs;
    }

	@Override
	public void setConfigValue(ConfigVO vo){
		try
		{
			Config c = em.find(Config.class, vo.getId());
			c.setConfigurationValue(vo.getConfigurationValue());
			
		}
		catch(Exception e){
			
			logger.error("Error al modificar la Configuracion");
			logger.error(e);
			
		}
	}
	
	@Override
	public String getConfigValue(String key){
		try
		{
			Query query = em.createQuery("from Config c where c.configurationKey = :configKey");
			query.setParameter("configurationKey",key);
			Config c = (Config) query.getSingleResult();
			if(c!=null) return c.getConfigurationValue();
			else return null;
				
				
		}
		catch(Exception e){			
			logger.error("Error al obtener la Configuracion");
			logger.error(e);
			
			return null;
		}
	}
    
}
